<?php

// 店铺搜索 商品搜索

namespace app\shop\controller;

use think\Cookie;
use think\Request;
use think\Controller;
use app\shop\model\Shop as ShopModel;
use app\shop\model\Limited as LimitedModel;
use app\shop\model\ShopType as ShopTypeModel;
use app\shop\model\GoodsType as GoodsTypeModel;
use app\shop\model\ShopGoods as ShopGoodsModel;
use app\shop\model\GroupGoods as GroupGoodsModel;
use app\shop\model\LimitedType as LimitedTypeModel;

class Search extends Controller
{
    /**
     * 搜索商品
     * @param Request $request
     * @param GoodsTypeModel $goodsType
     * @param ShopGoodsModel $shopGoods
     * @param LimitedTypeModel $limitedType
     * @param LimitedModel $limited
     * @param GroupGoodsModel $groupGoods
     * @return mixed
     */
    public function goods(Request $request, GoodsTypeModel $goodsType, ShopGoodsModel $shopGoods, LimitedTypeModel $limitedType, LimitedModel $limited, GroupGoodsModel $groupGoods)
    {
        // 获取参数
        $inquire = $request->param();

        // 商城 商品分类
        $category = $goodsType::all();

        // 搜索历史记录
        if (!empty($inquire['title'])) self::searchRecord($inquire['title']);

        // 筛选条件
        $condition['state'] = 1;
        if (!empty($inquire['title'])) $condition['title'] = ['like', '%' . $inquire['title'] . '%'];
        if (!empty($inquire['type'])) $condition['goods_str'] = ['like', '%,' . $inquire['type'] . ',%'];


        // 默认排序
        $sort = ['shop_sort' => 'desc', 'sort' => 'desc', 'create_time' => 'desc'];

        // 排序传参
        if (!empty($inquire['order'])) $sort = [$inquire['order'] => 'desc'];

        // 商品分类 对应的商品
        $result = $shopGoods->where($condition)->field('id,title,price,thumb_img,sale_number,rebate_percentage,hits,inventory,create_time')
            ->order($sort)->paginate(28, false, ['query' => $inquire]);

        // 如果商品是活动商品改变价格
        $limited_type = $limitedType->where('id', 2)->value('end_time');

        foreach ($result as &$value) {
            $limit_info = $limited->where('goods_id', $value['id'])->value('price');
            if ($limit_info && $limited_type > time()) {
                $value['price'] = $limit_info;
            }
            $special_price = $groupGoods->where(['goods_id' => $value['id'], 'status' => 1])->value('price');
            if ($special_price) {
                $value['price'] = $special_price;
            }
        }


        // 商品总数
        $total = $shopGoods->where($condition)->count();


        return $this->fetch('', [
            'mate_head' => '海外馆',
            'cates'     => get_cate($category),
            'data'      => $result,
            'total'     => $total,
        ]);
    }


    /**
     * 搜索店铺
     * @param Request $request
     * @param ShopModel $shopModel
     * @param ShopTypeModel $shopType
     * @return mixed
     */
    public function shops(Request $request, ShopModel $shopModel, ShopTypeModel $shopType)
    {
        // 获取参数
        $inquire = $request->param();

        // 搜索历史记录
        if (!empty($inquire['title'])) self::searchRecord($inquire['title']);

        // 筛选条件
        $condition['status'] = 1;
        if (!empty($inquire['title'])) $condition['title'] = ['like', '%' . $inquire['title'] . '%'];
        if (!empty($inquire['type'])) $condition['type_id'] = $inquire['type'];

        // 符合条件的店铺
        $result = $shopModel->where($condition)->field('title,member_id,logo,collect_number')->paginate(12, false, ['query' => $inquire]);

        // 店铺分类
        $shop_types = $shopType->where(['status' => 1])->field('id,title,picUrl')->order(['sort' => 'asc'])->select()->toArray();


        return $this->fetch('', [
            'mate_head'  => '店铺搜索',
            'data'       => $result,
            'shop_types' => $shop_types,
            'title'      => $inquire['title'],
        ]);
    }


    /**
     * 搜索记录记录
     * @param $info
     */
    public function searchRecord($info)
    {

        $new_info[] = $info;
        //是否含有搜索记录
        if (Cookie::has('search_record')) {

            $record = Cookie::get('search_record');

            //合并记录&检测是否有重复的记录
            foreach ($record as $k => $value) {

                if ($info == $value) {

                    $new_info = $record;

                    break;
                } else {

                    //删除最后一个记录
                    if (count($record) >= 10) {
                        array_pop($record);
                    }

                    $new_info[] = $value;
                }
            }
        }

        //存入cookie
        Cookie::set('search_record', $new_info);
    }
}